package util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import models.CNAverage;
import models.SensorData;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;

public class Persistence {

    private static final int FIRST = 0;
    private static final int MAX_CNA_LISTSIZE = 300;
    private static final int MAX_SENSORDATE_LISTSIZE = 100;

    private static ArrayList<CNAverage> averages = new ArrayList<CNAverage>();
    private static ArrayList<SensorData> sensorDatas = new ArrayList<SensorData>();

    public static void saveSordata(SensorData sensorData) {
        if (sensorDatas.size() > MAX_SENSORDATE_LISTSIZE) {
            sensorDatas.remove(FIRST);
        }
        sensorDatas.add(sensorData);
    }

    public static void saveCNA(CNAverage cnAverage) {
        if (averages.size() > MAX_CNA_LISTSIZE) {
            averages.remove(FIRST);
        }
        averages.add(cnAverage);
    }

    public static ArrayList<CNAverage> getAverages() {
        return averages;
    }

    public static ArrayList<SensorData> getSensorDatas() {
        return sensorDatas;
    }

    public static List<Integer> getLastFiveMeasurements(String uid) {
        ArrayList<Integer> lastFive = new ArrayList<Integer>();
        for (int i = sensorDatas.size(); i > 0;) {
            if (lastFive.size() >= 5) {
                break;
            }
            SensorData current = sensorDatas.get(--i);
            if (current.getSensorUID().equals(uid)) {
                lastFive.add(current.getSensorValue());
            }
        }
        return lastFive;
    }

    public static SensorData getLastMeasurement(String uid) {
        ArrayList<SensorData> lastData = new ArrayList<SensorData>();
        for (int i = sensorDatas.size(); i > 0;) {
            if (lastData.size() >= 1) {
                break;
            }
            SensorData current = sensorDatas.get(--i);
            if (current.getSensorUID().equals(uid)) {
                lastData.add(current);
            }
        }
        if (lastData.size() != 1) {
            return null;
        }
        return lastData.get(0);
    }

    public static CNAverage getLastCNAvaerage(String nodeIP, String measurementPointIP) {
        ArrayList<CNAverage> returnValue = new ArrayList<CNAverage>();
        for (int i = averages.size(); i > 0;) {
            if (returnValue.size() >= 1) {
                break;
            }
            CNAverage current = averages.get(--i);
            if (current.getNodeIP().equals(nodeIP) && current.getMeasurementPointIP().equals(measurementPointIP)) {
                returnValue.add(current);
            }
        }
        if (returnValue.size() != 1) {
            return null;
        }
        return returnValue.get(0);
    }
    
    public static CNAverage getLastCNAvaerage(String nodeIP) {
        ArrayList<CNAverage> returnValue = new ArrayList<CNAverage>();
        for (int i = averages.size(); i > 0;) {
            if (returnValue.size() >= 1) {
                break;
            }
            CNAverage current = averages.get(--i);
            if (current.getNodeIP().equals(nodeIP)) {
                returnValue.add(current);
            }
        }
        if (returnValue.size() != 1) {
            return null;
        }
        return returnValue.get(0);
    }

    public static CNAverage getLastCNAvaerage(TFSensorType sensorType) {
        ArrayList<CNAverage> returnValue = new ArrayList<CNAverage>();
        for (int i = averages.size(); i > 0;) {
            if (returnValue.size() >= 1) {
                break;
            }
            CNAverage current = averages.get(--i);
            if (current.getSensorType().equals(sensorType)) {
                returnValue.add(current);
            }
        }
        if (returnValue.size() != 1) {
            return null;
        }
        return returnValue.get(0);
    }

    public static List<CNAverage> getCNAsince(long sinceTimeStamp, TFSensorType sensorType, String nodeIP) {
        ArrayList<CNAverage> returnValue = new ArrayList<CNAverage>();
        for (int i = averages.size(); i > 0;) {
            CNAverage current = averages.get(--i);
            if (current.getTimestamp() < sinceTimeStamp) {
                // There are no more younger Elements in this list
                return returnValue;
            }

            if (current.getSensorType().equals(sensorType) && current.getNodeIP().equals(nodeIP)) {
                returnValue.add(current);
            }
        }
        return returnValue;
    }
}
